<?php
/**
 * Created by PhpStorm.
 * User: Inhere
 * Date: 15-4-1
 * Time: 上午10:08
 * Used: bootstrap 面包屑导航
 * file: Breadcrumb.php
 */

namespace ulue\ext\zui;

use Ulue;
use ulue\web;
use ulue\core\router;
use ulue\libs\front\Element;
use ulue\libs\front\Html;
use ulue\libs\helpers;

class Breadcrumb extends web\Widget
{
    /**
     * @var array
     */
    protected $data = [];

    /**
     * @var string
     */
    public $route;


    public function __construct()
    {
        $this->data  = $this->defaultConfigData();
        $this->route = \Ioc::get('router')->routeString;
    }

    public function setData(array $config)
    {
        $this->data = helpers\ArrayHelper::merge($this->data, $config);

        return $this;
    }

    /**
     * @return mixed
     */
    public function beginExecute()
    {
        $breadcrumbs  = Ulue::$app->getController()->breadcrumbs;

        $data = helpers\ArrayHelper::merge($this->data, $breadcrumbs);
        $title = '';

        if ( !empty($data['title']) ) {
            $title = $data['title'];
            $title = "<strong> $title </strong>";
        }

        $content =  $title;

        $mLink = new Element('a', null , $data['module']);
        $content .= Html::tag('li', (string)$mLink);

        $cLink = new Element('a', null , $data['controller']);
        $content .= Html::tag('li', (string)$cLink);

        $aLink = new Element('a', null , $data['action']);
        $content .= Html::tag('li', $aLink->content, ['class'=>'active']);

        $breadcrumb = Html::tag('ul', $content, $data['attrs'] );

        return $breadcrumb;
    }

    public function defaultConfigData()
    {
        $uri = \Ioc::get('app.uri');

        return [
            'title' => 'Position: ',
            'attrs' => [
                'class'=>'breadcrumb',
                'style'=>"margin:5px 0;"
            ],
            'module' => [
                'text'  => Ulue::$app->getModule()->getName(),
                'href'  => $uri->get('module.path') ,
                // 'icon'  => 'icon-home'
            ],
            'controller' => [
                'text'  => Ulue::$app->getController()->getName(),
                'href'  => $uri->get('controller.path') ,
                // 'attrs' => [],
            ],
            'action' => [
                'text'  => Ulue::$app->getController()->getActionName(),
                'href'  => $uri->get('action.path') ,
                // 'attrs' => [],
            ]
        ];
    }
}